package com.softserve.academy.food.controllers.admin;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.softserve.academy.food.model.mDish;
import com.softserve.academy.food.model.mDishType;
import com.softserve.academy.food.services.admin.iDishManagerService;

@Controller
@RequestMapping( value="/managerdishs" )
public class DishManager  
{
	@Autowired
	private iDishManagerService dService;
	private ModelAndView dModel;
	private String id;
	private Boolean cheked = true;
	
	@RequestMapping( method = RequestMethod.GET)
	public ModelAndView info()
	{
		dModel = new ModelAndView("admin");
		return dModel;
	}
	
	@RequestMapping( params={"action=listdishs"}, method = RequestMethod.GET )
	public ModelAndView showListDishs()
	{
		dModel = new ModelAndView("admin");
		dModel.addObject( "listdishs" , dService.getDishs() );
		
		return dModel;
	}
	
	@RequestMapping( params={"action=adddish"}, method = RequestMethod.GET )
	public ModelAndView addDish( HttpServletRequest request )
	{
		if ( cheked )
		{
			checkDir( request );
		}
		
		dModel = new ModelAndView("admin");
		dModel.addObject( "dish" , new mDish() );
		dModel.addObject( "listtype" , dService.getDishTypes() );
		
		return dModel;
	}
	
	@RequestMapping( params={"action=dishedit"}, method = RequestMethod.GET )
	public ModelAndView showDish()
	{
		dModel = new ModelAndView("admin");
		dModel.addObject( "dish", dService.getDish(id) );
		dModel.addObject( "listtype" , dService.getDishTypes() );
		
		return dModel;
	}
	
	@RequestMapping( params={"action=editcategory"}, method = RequestMethod.GET )
	public ModelAndView showCategory()
	{
		dModel = new ModelAndView("admin");
		dModel.addObject( "listtype" , dService.getDishTypes() );
		dModel.addObject( "dishtype" , new mDishType() );
		
		return dModel;
	}
	
	@RequestMapping( params={"action=deldish"}, method = RequestMethod.GET )
	public ModelAndView viewDelDish()
	{
		dModel = new ModelAndView("admin");
		dModel.addObject( "dish", dService.getDish(id) );
		
		return dModel;
	}
	
	@RequestMapping( params={"action=adddish"}, method = RequestMethod.POST )
	public String addDish( @RequestParam("file") MultipartFile file, @ModelAttribute("mDish") mDish dish )
	{
		dService.addDish( dish,  file );
		return "redirect:/managerdishs.htm?action=adddish";
	}
	
	@RequestMapping( params={"action=editcategory"}, method = RequestMethod.POST)
	public String editCategory( @ModelAttribute("mDishType") mDishType type, HttpServletRequest request )
	{
		id = request.getParameter("id");
		
		if (id!=null)
		{
			dService.delDishType(id);
		}
		else
		{
			dService.addDishType(type);
		}
		
		return "redirect:/managerdishs.htm?action=editcategory";
	}
	
	@RequestMapping( params={"action=deldish"}, method = RequestMethod.POST )
	public String showDel( HttpServletRequest request )
	{
		if ( request.getParameter("acept")!=null && request.getParameter("acept").equals("YES") )
		{
			dService.delDish(id);
			return "redirect:/managerdishs.htm?action=listdishs";
		}
		else
		{
			id = request.getParameter("id");
		}
		
		return "redirect:/managerdishs.htm?action=deldish";
	}
	
	@RequestMapping( params={"action=listdishs"}, method = RequestMethod.POST )
	public String delDish( HttpServletRequest request )
	{
		id = request.getParameter("id");
		
		return "redirect:/managerdishs.htm?action=dishedit";
	}
	
	private void checkDir( HttpServletRequest request )
	{
		if (dService.getRoot_dir()==null)
		{
			dService.setRoot_dir( request.getSession().getServletContext().getRealPath("/") );
			cheked = false;
		}
	}
	
	@RequestMapping( params={"action=dishedit"}, method = RequestMethod.POST )
	public String userEdit( @ModelAttribute("mDish") mDish dish )
	{
		dService.updateDish( dish );
		return "redirect:/managerdishs.htm?action=listdishs";
	}
	
}

